#!/usr/bin/python
import sys , time , threading
sys.path.append("../classes")
sys.path.append("../classes/plugins")
from groupmanager import *
from logger import *
import jsonparse as pm
parser = pm.Parser() 
logger = Logger("../logs")
logger.setVerbose()

group_conf = {"fields": ["a","b","c"] , "splitter":"^^^" , "sumfields":{"Z1":"avg","Z2":"sum"} , "epocs":[5,11] , "faults_min":1 , "file":"lines1.log"}
compare_conf = {"minval" : 0 , "rates" : {"count":0.5 , "Z1" : 0.7 , "Z2": 0.7}}


#############################################################3333

def _print(gm):
	for k in gm.Pulses:
		print("--------		%s / %s	  --------------" % (gm.schedular[k] if k in gm.schedular else 0 , k))
		print("%s\n" % gm.Pulses[k].Persist)
	print("faults:\n%s\n=================================================\n" % gm.faults)

#############################################################3333

def write_lines(filename , list):
	f = open(filename , "w")
	for line in list : f.write("%s\n" % json.dumps(line))
	f.close()

#############################################################3333


st = 1	# sleep seconds


lines1 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"},
{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.1" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"3","Z2":"0.2" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"2","Z2":"0.3" },]

lines2 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"0.6" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]


write_lines("lines1.log" , lines1)
write_lines("lines2.log" , lines2)

print "GENERAL TEST"


gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()                                                               
time.sleep(st)
gm.pulse()



print "faults should have only single row"
assert(len(gm.faults.keys()) == 1)
print "OK"

print "in k = 'a^^^b^^^c' count decreased from 4 to 1 and compare_conf['rates']['count'] is set to 0.5 so foreach epoc in group_conf['epocs'] increase fault e.g faults['count'] == 2"
assert (gm.faults['a^^^b^^^c']['count'] == 2)
print "OK"

print "in k = 'a^^^b^^^c' Z1 is ok as last pulse was 14 verse 3.75 from first. assert no Z1 in gm.faults['a^^^b^^^c']"
assert("Z1" not in gm.faults['a^^^b^^^c'])
print "OK"


group_conf["file"] = "lines1.log"
gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()


print "in k = 'a^^^b^^^c' Z2 first pulse sum is 0.9 second is 0.6 so 0.6 / 0.9 < 0.7 set in compare_conf['rates']['Z2'] so assert gm.faults['a^^^b^^^c']['Z2'] == 2"
assert(gm.faults['a^^^b^^^c']['Z2'] == 2)
print "OK"



print "adding another pulse ..."
print "this time we enter just one key the previuse key had faults entries to be anulated."
print "since we dont enter another keys they all should be faulted"

lines3 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"1.1" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"1.5" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"1.2" },]

write_lines("lines3.log" , lines3)
group_conf["file"] = "lines3.log"


threading.Thread(target=gm.run).start()
time.sleep(st)

gm.pulse()


print "assert there are 2 lines in faults and no key 'a^^^b^^^b'"
assert(len(gm.faults.keys()) == 2)
assert(not 'a^^^b^^^b' in gm.faults)
print "OK"


print "Testing sum fields..."

st = 1  # sleep seconds

group_conf = {"fields": [""] , "splitter":"^^^" , "sumfields":{"Z1":"avg","Z2":"sum"} , "epocs":[5,11] , "faults_min":1 , "file":"lines1.log"}
compare_conf = {"minval" : 1 , "rates" : {"count":0.9 , "Z1" : 1.2 , "Z2": 0.8}}

lines1 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]

lines2 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"0.6" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"1"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"1"}]


write_lines("lines1.log" , lines1)
write_lines("lines2.log" , lines2)

gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()                                                               
time.sleep(st)
gm.pulse()


print "faults should have only single row"
assert(gm.faults['']['Z1'] == 2)
assert(gm.faults['']['Z2'] == 2)
print "OK"

print "changing the comparer levels..."
compare_conf = {"minval" : 1 , "rates" : {"count":0.9 , "Z1" : 5.0 , "Z2": 0.1}}
group_conf["file"] = "lines1.log"

gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

assert(len(gm.faults.keys()) == 0)
print "OK"



print "remote ssh log file "
st = 10

group_conf = {"fields": ["a","cc","p"] , "splitter":"^^^" , "sumfields":{"uid":"dist"} , "epocs":[5,11,7] , "faults_min":4 , "file":"/mnt/ramdisk/facemoods/searches.log" , "parser":"jsonparse" , "ssh":{"host":"start.a.nano.funmoods.com","user":"nginx","passwd":""}}
compare_conf = {"minval" : 8 ,  "rates" : {"count":0.8 , "uid" : 0.8 }}

gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
for i in range(2):
	print "%s time" % (i)
	threading.Thread(target=gm.run).start()
	time.sleep(st)
	gm.RUNNER = False
	time.sleep(2)
	gm.pulse()


######################################################33


print "FILTER TEST..."

group_conf = {"monitor":"true", "fields": ["a","b","c"] , "splitter":"^^^" , "sumfields":{"Z1":"avg","Z2":"sum"} , "epocs":[5,11] , "faults_min":1 , "file":"lines1.log","filter":{"a":"a","b":"b","c":"c"}}
compare_conf = {"minval" : 0 , "rates" : {"count":0.5 , "Z1" : 0.7 , "Z2": 0.7}}

st = 1  # sleep seconds


open("../logs/monitor.log","w").close()


lines1 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.3" },
{"a":"aa","b":"bb","c":"c","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"},
{"a":"a","b":"b","c":"c","d":"d","Z1":"5","Z2":"0.1" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"3","Z2":"0.2" },
{"a":"a","b":"b","c":"c","d":"d","Z1":"2","Z2":"0.3" },]

lines2 = [{"a":"a","b":"b","c":"c","d":"d","Z1":"15","Z2":"0.6" },
{"a":"aa","b":"bb","c":"cc","d":"dd","Z2":"3"},
{"a":"aaa","b":"bbb","c":"ccc","d":"ddd" , "Z1":"15","Z2":"3"}]


write_lines("lines1.log" , lines1)
write_lines("lines2.log" , lines2)

print "GENERAL TEST"


gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

print "should be 2 rows in monitor.log ..."
c = 0
f = open("../logs/monitor.log","r")
for line in f:
	c += 1
assert(c == 2)
print "OK"


open("../logs/monitor.log","w").close()

group_conf["file"] = "lines1.log"
group_conf["filter"] = {"c":"c"}
gm = GroupManager("test1" , group_conf , compare_conf , parser , logger)
threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()

group_conf["file"] = "lines2.log"

threading.Thread(target=gm.run).start()
time.sleep(st)
gm.pulse()


print "should be 3 rows in monitor.log ..."
c = 0
f = open("../logs/monitor.log","r")
for line in f:
        c += 1
assert(c == 2)
print "OK"

sys.exit(0)

#################################################################

print "Testing Pair with 2 ssh hosts..."

st = 10

group_conf = {"fields": ["a","cc","p"] , "splitter":"^^^" , "sumfields":{"uid":"dist"} , "epocs":[5,11,7] , "faults_min":4 , "file":"/mnt/ramdisk/facemoods/searches.log" , "parser":"jsonparse" , "ssh":{"hosts":["start.a.nano.funmoods.com", "start.b.nano.funmoods.com" ] , "user":"nginx","passwd":""}}
compare_conf = {"minval" : 8 ,  "rates" : {"count":0.8 , "uid" : 0.8 }}

gm = PairGroupManager("test1" , group_conf , compare_conf , parser , logger)
for i in range(2):
        print "%s time" % (i)
        threading.Thread(target=gm.run , args=(0,)).start()
        threading.Thread(target=gm.run , args=(1,)).start()
        time.sleep(st)
        gm.RUNNER = False
        time.sleep(2)
        gm.pulse()


#print gm.print_alerts()


#_print(gm)

